import 'package:flutter/material.dart';
import 'package:flutter_animation/Utils/SPUtil.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:flutter_svg/flutter_svg.dart';

import '../Utils/ColorUtil.dart';
import 'SettingBean.dart';
import '../Login/Login.dart';
import '../Utils/DB/MessageDao.dart';

class MinePage extends StatefulWidget{
  const MinePage({super.key});

  @override
  State<StatefulWidget> createState() {
    return _MinePageState();
  }

}

class _MinePageState extends State<MinePage> {

  int index = -1;

  late List<SettingBean> functions = [
    const SettingBean(Colors.deepOrange, 'assets/images/gift.svg', '邀请有礼'),
    const SettingBean(Colors.purple, 'assets/images/translate.svg', '语言'),
    const SettingBean(ColorUtil.primary, 'assets/images/share.svg', '分享Monica'),
    const SettingBean(Colors.deepPurple, 'assets/images/message.svg', '联系我们'),
    const SettingBean(Colors.orangeAccent, 'assets/images/info.svg', '删除聊天记录'),
  ];

  @override
  Widget build(BuildContext context) {
    return Stack(
      alignment: AlignmentDirectional.topCenter,
      children: [
        Image.asset(
          'assets/images/settings_page_bg.png',
        ),
        Column(
          children: [
            Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                SizedBox(height: MediaQuery.of(context).padding.top + 20),
                GestureDetector(
                  behavior: HitTestBehavior.opaque,
                  onTap: () {
                    SmartDialog.showToast('点击头像',displayType: SmartToastType.last);
                  },
                  child: const CircleAvatar(
                    radius: 50,
                    backgroundColor: Colors.transparent,
                    backgroundImage: AssetImage(
                      'assets/images/doubao_avatar.png',
                    ),
                  ),
                ),
                const SizedBox(height: 20),
                GestureDetector(
                  behavior: HitTestBehavior.opaque,
                  onTap: () {
                    SmartDialog.showToast('点击昵称',displayType: SmartToastType.last);
                  },
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: [
                      const Text(
                        '豆包',
                        style: TextStyle(
                          color: Colors.black,
                          fontSize: 16,
                          fontWeight: FontWeight.bold,
                        ),
                      ),
                      SvgPicture.asset('assets/images/arrow_right.svg')
                    ],
                  ),
                ),
                const SizedBox(height: 10),
                GestureDetector(
                  behavior: HitTestBehavior.opaque,
                  onTap: (){
                    SmartDialog.showToast('点击邮箱',displayType: SmartToastType.last);
                  },
                  child: const Text(
                    "doubao.com",
                    style: TextStyle(
                      color: Colors.grey,
                      fontSize: 12,
                    ),
                  ),
                ),
              ],
            ),
            Expanded(
              child: Padding(
                padding: const EdgeInsets.fromLTRB(15, 20, 15, 15),
                child: Column(
                  mainAxisAlignment: MainAxisAlignment.start,
                  children: [
                    Row(
                      children: [
                        Expanded(
                          flex: 1,
                          child: buildFunctionRow('assets/images/vip_yellow.svg','升级'),
                        ),
                        const SizedBox(
                          width: 15,
                        ),
                        Expanded(
                          flex: 1,
                          child: buildFunctionRow('assets/images/usage.svg','用量'),
                        ),
                      ],
                    ),
                    Expanded(
                      child: Padding(
                        padding: const EdgeInsets.symmetric(vertical: 15),
                        child: Padding(
                          padding: const EdgeInsets.only(bottom: 5),
                          child: Container(
                            decoration: BoxDecoration(
                              color: Colors.white,
                              borderRadius: BorderRadius.circular(10),
                              boxShadow: const [
                                BoxShadow(
                                  color: Color(0xFFE8E8E8),
                                  offset: Offset(1,3),
                                  blurRadius: 2,
                                  spreadRadius: 0,
                                ),
                              ],
                            ),
                            child: MediaQuery.removePadding(
                              context: context,
                              removeTop: true,
                              child: ListView.builder(
                                scrollDirection: Axis.vertical,
                                shrinkWrap: true,
                                itemCount: functions.length,
                                itemExtent: 65,
                                physics: const BouncingScrollPhysics(),
                                itemBuilder: (context, index) {
                                  return ClipRRect(
                                    borderRadius: index == 0 ?
                                    const BorderRadius.vertical(top: Radius.circular(10)) :
                                    index == 4 ? const BorderRadius.vertical(bottom: Radius.circular(10)) :
                                    const BorderRadius.vertical(bottom: Radius.circular(0)),
                                    child: Material(
                                      color: Colors.transparent,
                                      child: Ink(
                                        decoration: BoxDecoration(
                                          borderRadius: index == 0 ?
                                          const BorderRadius.vertical(top: Radius.circular(10)) :
                                          index == 4 ? const BorderRadius.vertical(bottom: Radius.circular(10)) :
                                          const BorderRadius.vertical(bottom: Radius.circular(0)),
                                        ),
                                        child: ListTile(
                                          splashColor: ColorUtil.color999999_10,
                                          contentPadding: const EdgeInsets.symmetric(horizontal: 15),
                                          leading: Container(
                                            padding: const EdgeInsets.all(9),
                                            decoration: BoxDecoration(
                                              borderRadius: const BorderRadius.all(Radius.circular(50)),
                                              color:functions[index].color,
                                            ),
                                            child: SvgPicture.asset(
                                              functions[index].picPath,
                                              width: 15,
                                              height: 15,
                                            ),
                                          ),
                                          title: Text(
                                            functions[index].text,
                                            style: const TextStyle(
                                              fontSize: 14,
                                            ),
                                          ),
                                          titleAlignment: ListTileTitleAlignment.center,
                                          trailing: SvgPicture.asset(
                                            'assets/images/arrow_right.svg',
                                            colorFilter: const ColorFilter.mode(Colors.grey, BlendMode.srcIn),
                                          ),
                                          onTap: () {
                                            if (index == 4) {
                                              // SPUtil.deleteHistory();
                                              MessageDao.deleteAll();
                                              SmartDialog.showToast('删除成功',displayType: SmartToastType.last);
                                            }
                                          },
                                        ),
                                      ),
                                    ),
                                  );
                                },
                              ),
                            ),
                          ),
                        ),
                      ),
                    ),
                    TextButton(
                      style: const ButtonStyle(
                        backgroundColor: WidgetStatePropertyAll(ColorUtil.primary),
                        overlayColor: WidgetStatePropertyAll(ColorUtil.colorWhite_20),
                        shape: WidgetStatePropertyAll(
                          RoundedRectangleBorder(
                            borderRadius: BorderRadius.all(Radius.circular(10)),
                          ),
                        ),
                      ),
                      onPressed: () {
                        SPUtil.logout();
                        if (mounted) {
                          Navigator.pushAndRemoveUntil(
                            context,
                            MaterialPageRoute(
                              builder: (context) => const Login(),
                            ), (route) => false,
                          );
                        }
                      },
                      child: const SizedBox(
                        width: double.infinity,
                        child: Text(
                          '退出登陆',
                          textAlign: TextAlign.center,
                          style: TextStyle(
                            color: Colors.white,
                            fontSize: 16,
                          ),
                        ),
                      ),
                    ),
                  ],
                ),
              ),
            ),
          ],
        ),
      ],
    );
  }

  Widget buildFunctionRow(String iconPath, String text) {
    return Container(
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.circular(10),
        boxShadow: const [
          BoxShadow(
            color: Color(0xFFE8E8E8),
            offset: Offset(1,3),
            blurRadius: 2,
            spreadRadius: 0,
          ),
        ],
      ),
      child: TextButton(
        onPressed: (){

        },
        style: ButtonStyle(
          overlayColor: const WidgetStatePropertyAll(ColorUtil.color999999_10),
          backgroundColor: const WidgetStatePropertyAll(
            Colors.white,
          ),
          shape: WidgetStatePropertyAll(
            RoundedRectangleBorder(
              borderRadius: BorderRadius.circular(10),
            ),
          ),
          padding: const WidgetStatePropertyAll(
            EdgeInsets.symmetric(horizontal: 8,vertical: 15),
          ),
        ),
        child: Row(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Expanded(
              child:SvgPicture.asset(
                iconPath,
              ),
            ),
            Expanded(
              flex: 1,
              child: Text(
                text,
                style: const TextStyle(
                  color: Colors.black,
                  fontSize: 13,
                  fontWeight: FontWeight.bold,
                ),
              ),
            ),
            Expanded(
              child: SvgPicture.asset(
                'assets/images/arrow_right.svg',
                colorFilter: const ColorFilter.mode(Colors.grey, BlendMode.srcIn),
              ),
            ),
          ],
        ),
      ),
    );
  }
}